//
//  LoginViewController.m
//  UI_豆瓣更新版
//
//  Created by dllo on 16/1/2.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>

@end

@implementation LoginViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"登录";
    
    LoginView *view = [[LoginView alloc] initWithFrame:CGRectMake(35, 70, 305, 50)];
    view.myLabel.text = @"用户名:";
    view.myTextField.placeholder = @"请输入用户名";
    view.myTextField.tag = 1000;
    view.myTextField.delegate = self;
    [self.view addSubview:view];
    [view release];
    
    LoginView *view1 = [[LoginView alloc] initWithFrame:CGRectMake(35, 170, 305, 50)];
    view1.myLabel.text = @"密码:";
    view1.myTextField.placeholder = @"请输入密码";
    view1.myTextField.secureTextEntry = YES;
    view1.myTextField.delegate = self;
    [self.view addSubview:view1];
    [view1 release];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(35, 300, 140, 50);
    button1.backgroundColor = [UIColor greenColor];
    [button1 setTitle:@"登录" forState:UIControlStateNormal];
    button1.layer.cornerRadius = 10;
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(200, 300, 140, 50);
    button2.backgroundColor = [UIColor orangeColor];
    [button2 setTitle:@"注册" forState:UIControlStateNormal];
    button2.layer.cornerRadius = 10;
    [self.view addSubview:button2];
    [button2 addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
}

// 返回按钮方法
- (void)buttonAction:(UIBarButtonItem *)item {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
}
// 登录按钮方法
- (void)buttonAction1:(UIButton *)button {
    _block(@"注销");
    
    UITextField *tf = [self.view viewWithTag:1000];
    _block1(tf.text);
    
    if (!tf.text.length) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    // 控制提示框时间(afterDelay  延时)
    [self performSelector:@selector(dismissAlert) withObject:nil afterDelay:1.5];

}

- (void)dismissAlert {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// 注册按钮方法
- (void)buttonAction2:(UIButton *)button {
    RegisterViewController *regVc = [[RegisterViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:regVc];
    [self presentViewController:nav animated:YES completion:^{
        
    }];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
